package com.xj.api.bussiness.deviceImg.web;

import com.xj.api.bussiness.deviceImg.service.ITbDeviceImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xj.api.base.web.BaseController;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.bussiness.deviceImg.entity.TbDeviceImg;

import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *   设备的照片表（如设备正前方，右后方）的照片  前端控制器
 * </p>
 *
 * @author zhaohai
 * @since 2018-01-27
 */
@RestController
@RequestMapping("deviceImg")
public class TbDeviceImgController extends BaseController{
	
	@Autowired
	private ITbDeviceImgService deviceImgService;
	
	@GetMapping
	@ApiOperation(value = "设备的照片列表", httpMethod = "GET", notes = "  列表")
	public AbstractBean deviceImgList(@RequestParam(required=false,defaultValue="0") Integer pageNum,@RequestParam(required=false,defaultValue="10") Integer pageSize){
		return json(deviceImgService.selectMapsPage(new Page<TbDeviceImg>(pageNum, pageSize), new EntityWrapper<TbDeviceImg>()));
	}
	
	@GetMapping("/{id}.auth")
	@ApiOperation(value = "设备的照片詳情", httpMethod = "GET", notes = "  詳情")
	public AbstractBean deviceImgContent(@PathVariable Integer id){
		return json(deviceImgService.selectById(id));
	}
	
	@PostMapping("save")
	@ApiOperation(value = "设备的照片新增", httpMethod = "POST", notes = " 新增")
	public AbstractBean add(TbDeviceImg tbdeviceimg) {

//		final String UL = "http://119.23.111.186/uploads/img";
//		Map<String, String> uploads = ImageUploadUtil.upload(req,UL+tbdeviceimg.getBigShelfNum());
//		//获得tbdeviceimg对象的属性名称
//		Field[] fields = tbdeviceimg.getClass().getDeclaredFields();
//		for (Map.Entry<String, String> entry : uploads.entrySet()) {
//			//Map.entry<Integer,String> 映射项（键-值对）  有几个方法：用上面的名字entry
//			//entry.getKey() ;entry.getValue(); entry.setValue();
//			//map.entrySet()  返回此映射中包含的映射关系的 Set视图。
//			//System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
//			for (Field field:fields) {
//				field.setAccessible(true);
//				//和上传完返回的路径坐对比将map的kye和对象中的属性的名称做对比如果符合则把返回的map中的value值
//				//赋值给相对应的图标
//				if(entry.getKey().equals(field.getName())){
//					field.set(tbdeviceimg,entry.getValue());
//				}
//			}
//		}

		tbdeviceimg.setCreateDate(new Date());
		if(!deviceImgService.insertOrUpdate(tbdeviceimg)){
			return error();
		}
		return success();
	}
	@DeleteMapping("{id}/delete")
	@ApiOperation(value = "  删除", httpMethod = "POST", notes = " 删除")
    public AbstractBean delete(@PathVariable(required=true) Integer id) {	
		if(!deviceImgService.deleteById(id)){
		return error();
		}
		return success();
    }
    @PostMapping("getDeviceImgByCondition")
	@ApiOperation(value ="根据设备id,大架号查询设备的照片表" , httpMethod = "POST" ,notes = "根据设备id,大架号查询设备的照片表")
	public AbstractBean getDeviceImgByCondition(TbDeviceImg tbDeviceImg){
		List<TbDeviceImg> list = deviceImgService.getDeviceImagByCondition(tbDeviceImg);
		if (list != null && list.size()>0){
			return SuccessMessage("用户存在","1");
		}else {
			return fail("用户不存在","0");
		}
	}

	@PostMapping("getNewDeviceImgByEntity")
	@ApiOperation(value ="根据设备id,大架号查询设备的照片表" , httpMethod = "POST" ,notes = "根据设备id,大架号查询设备的照片表")
	public AbstractBean getNewDeviceImgByEntity(TbDeviceImg tbDeviceImg){
		List<TbDeviceImg> list = deviceImgService.getNewDeviceImgByEntity(tbDeviceImg);
		if (list != null && list.size()>0){
			return json(list);
		}else {
			return error();
		}
	}
}
